#!/bin/bash
#Author: nicheng
#Update: 2020-07-17
#Description: The script is used to deploy seo website.
#set -euxo pipefail
start=$(date +%s) 
copysiteconf=/www/seo/wwwconfig/www.renoty.com.conf
copysitename=renoty.com
siteconfdir=/www/seo/wwwconfig
phpconfdir=/www/seo/wwwroot
deploysitedir=/root/nicheng
deploysitefile=/root/nicheng/sitename
deploysitedb=/root/nicheng/sitedb
a=0;b=0;c=0
#部署的站点统一写成域名wap.szsgzsh.com   www.dldipingshigong.com   mip.acadsoc.com.cn的形式。
[ -d $siteconfdir -a -d $phpconfdir -a -f $copysiteconf ] || {
   echo -e "\033[43;31m站点的配置文件目录${siteconfdir}或站点的config.php配置文件主目录${phpconfdir}或复制站点的配置文件${copysiteconf}不存在,请检查后再重新部署.\033[0m"
   exit 1
}
[ -d $deploysitedir ] || mkdir $deploysitedir 2> /dev/null
[ -f $deploysitefile ] || touch $deploysitefile 2> /dev/null
[ -f $deploysitedb ] || touch $deploysitedb 2> /dev/null
copysitecontent=`cat $copysiteconf  |wc -l`
if [ x$copysitecontent == x0 ];then
    echo -e "\033[43;31m复制的站点的配置文件${copysiteconf}的内容为空,请检查后再重新部署.\033[0m"
    exit 2
fi
deploysitefilecontent=`cat $deploysitefile  |grep -v "^[ ]*$" |wc -l`
dpfiledcount1=`awk '{print $1}' $deploysitefile |grep -v "^[ ]*$" |wc -l`
dpfiledcount2=`awk '{print $2}' $deploysitefile |grep -v "^[ ]*$" |wc -l`
if [ $deploysitefilecontent -eq 0 ];then
    echo -e  "\033[43;31m部署站点的文件${deploysitefile}的内容为空,请检查后再重新部署.\033[0m"
    exit 3
else
    if [ $dpfiledcount1 != $dpfiledcount2 ];then
        echo -e "\033[43;31m部署站点的文件${deploysitefile}中缺少相应的参数,请检查.\033[0m"
        exit 4
    fi
fi
while read sourcecodedir newsite
do
    newsitecount=$(echo $newsite |egrep  '[a-Z|0-9|-]+\.[a-Z|0-9|-]+\.[a-Z|0-9|-]+' |wc -l) || true
    if [ x$newsitecount != x1 ];then
        echo  -e "\033[43;31m部署站点的文件${deploysitefile}中的站点${newsite}有问题,请检查.\033[0m"
        exit 5
    fi
done < $deploysitefile
while read sourcecodedir newsite
do
    configphpdir=$phpconfdir/$newsite
    sourcecodebag=$sourcecodedir/${newsite}.zip
    nginxconf=$siteconfdir/${newsite}.conf
    if [ -f $configphpdir/e/config/config.php -o -f $configphpdir/application/config/database.php -o -f $configphpdir/wp-config.php ];then
        if [ -f $nginxconf ];then
            continue
        fi
    fi
    if [ ! -f $sourcecodedir/${newsite}.zip ];then
        sitedb=${newsite#*.}
        sourcecodebag=$sourcecodedir/${sitedb}.zip
        if [ ! -f $sourcecodebag ];then
            echo -e "\033[43;31m源代码包${sourcecodebag}不存在,请检查后再重新部署此站点${newsite}.\033[0m"
            continue
        fi
    fi
    if [ -f $configphpdir ];then
        continue
    fi
    unzip -tq $sourcecodebag  1>& /dev/null
    if [ $? -ne 0 ];then
        echo -e "\033[43;31m部署的站点的源代码包${sourcecodebag}有问题,请检查后再重新部署此站点${newsite}.\033[0m"
        continue
    fi
    unzip -oq $sourcecodebag -d $configphpdir &
    echo -e "\033[41;34m源代码包${sourcecodebag}正在解压......\033[0m"
done < $deploysitefile
while read sourcecodedir newsite
do
    configphpdir=$phpconfdir/$newsite
    sourcecodebag=$sourcecodedir/${newsite}.zip
    nginxconf=$siteconfdir/${newsite}.conf
    if [ -f $configphpdir/e/config/config.php -o -f $configphpdir/application/config/database.php -o -f $configphpdir/wp-config.php ];then
        if [ -f $nginxconf ];then
            continue
        fi
    fi
    if [ ! -f $sourcecodedir/${newsite}.zip ];then
        sitedb=${newsite#*.}
        sourcecodebag=$sourcecodedir/${sitedb}.zip
        if [ ! -f $sourcecodebag ];then
            continue
        fi
    fi
    programnub=$(ps -aux |grep "$sourcecodebag" |grep -v "grep --color=auto" |wc -l) || true
    until [ $programnub -eq 0 ]
    do
        sleep 1
        programnub=$(ps -aux |grep "$sourcecodebag" |grep -v "grep --color=auto" |wc -l) || true
    done    
    echo  -e "\033[41;34m源代码包${sourcecodebag}解压成功.\033[0m"
done < $deploysitefile
while read sourcecodedir newsite
do
    configphpdir=$phpconfdir/$newsite
    nginxconf=$siteconfdir/${newsite}.conf
    if [ -f $configphpdir/e/config/config.php -o -f $configphpdir/application/config/database.php -o -f $configphpdir/wp-config.php ];then
        if [ -f $nginxconf ];then
            continue
        fi
    fi
    if [ -f $sourcecodedir/${newsite}.zip ];then
        sitedb=${newsite#*.}
        sitenamedb=${sitedb%.*}
        sourcecodebag=$sourcecodedir/${newsite}.zip
    else
        sitedb=${newsite#*.}
        sitenamedb=${sitedb%.*}
        sourcecodebag=$sourcecodedir/${sitedb}.zip
    fi
    newsitecount=$(echo $sitenamedb |egrep '[a-Z|0-9|-]+\.[a-Z|0-9|-]+' |wc -l) || true
    until [ x$newsitecount == x0 ]
    do
        sitedb=${sitedb#*.}
        sitenamedb=${sitedb%%.*}
        newsitecount=$(echo $sitenamedb |egrep '[a-Z|0-9|-]+\.[a-Z|0-9|-]+' |wc -l) || true
    done
    until [ -f ${configphpdir}/application/config/database.php -o -f ${configphpdir}/e/config/config.php ]
    do
        codedirnumber=`ls ${configphpdir} |wc -l`
        if [ x${codedirnumber} == x1 ];then
            codedir=`ls ${configphpdir}`
            cd ${configphpdir}/${codedir} && mv -bf *  ${configphpdir}
            cd ${configphpdir} && rm -rf $codedir
        else
            echo -e "\033[43;31mphp配置文件的目录${configphpdir}下文件有问题,请检查后再重新部署此站点${newsite}.\033[0m"
            break
        fi
    done
    if [ -f ${configphpdir}/application/config/database.php -o -f ${configphpdir}/e/config/config.php ];then
        echo "$sitedb" > testdb
        sed -i 's/\./_/g' testdb
        sitename=`awk '{print $1}' testdb`
        STNAME=`echo ${sitename} |grep '^[0-9]'` || true
        [ x$sitename == x$STNAME ] &&  sitename=sql_$sitenamedb
        if [ -f ${configphpdir}/e/config/config.php ];then
            phpconf=${configphpdir}/e/config/config.php
            sed -i -e '/dbserver/s/localhost/rm-j6cadguk11ixykhnr.mysql.rds.aliyuncs.com/' -e '/dbusername/s/root/marketseodbopa/' -e "/dbpassword/s|=''|='SDFGiseo@!#6786SEO'|"  -e "/dbname/s/ecms75/$sitename/"  $phpconf  && {
                echo -e "\033[41;34m配置文件${phpconf}修改成功.\033[0m" 
            } || {
                echo -e "\033[43;31m配置文件${phpconf}修改失败,请检查后再重新部署此站点${newsite}.\033[0m"
                continue
            }
        elif [ -f $configphpdir/application/config/database.php ];then
            phpconf=$configphpdir/application/config/database.php
            sed -i -e "s#'hostname' => '.*',#'hostname' => 'rm-j6cadguk11ixykhnr.mysql.rds.aliyuncs.com',#g" -e "s#'username' => '.*',#'username' => 'marketseodbopa',#g" -e  "s|'password' => '.*',|'password' => 'SDFGiseo@!#6786SEO',|g" -e "s#'database' => '.*',#'database' => '$sitename',#g" $phpconf && {
                echo -e "\033[41;34m配置文件${phpconf}修改成功.\033[0m" 
            } || {
                echo -e "\033[43;31m配置文件${phpconf}修改失败,请检查后再重新部署此站点${newsite}.\033[0m"
                continue
            }
        fi
        sed -i "/$sitename/d" $deploysitedb
        echo  "$sitename" >> $deploysitedb
        chown -R www:www $configphpdir
        cp -a $copysiteconf $nginxconf && {
            sed -i "s/www.$copysitename/$newsite/g" $nginxconf
            newsite=${newsite#*.}
            sed -i "s/$copysitename/$newsite/g" $nginxconf
            echo -e "\033[41;34m配置文件${nginxconf}修改成功.\033[0m"
        } || {
            echo -e "\033[43;31m配置文件${nginxconf}修改失败,请检查后再重新部署此站点${newsite}.\033[0m"
        }
    fi
done < $deploysitefile
while read sourcecodedir newsite
do
    let a+=1
    configphpdir=$phpconfdir/$newsite
    sourcecodebag=$sourcecodedir/${newsite}.zip
    nginxconf=$siteconfdir/${newsite}.conf
    if [ -f $configphpdir/e/config/config.php -o -f $configphpdir/application/config/database.php ];then
        if [ -f $nginxconf ];then
        let b+=1
        curl -IL http://localhost -H Host:${newsite} 1>& /dev/null && {
            TESTSITE=`curl -IL http://localhost -H Host:${newsite} 2> /dev/null |awk '/HTTP\/1.1/{print $2}'`
            if [ $TESTSITE == 200 ];then
                let c+=1
                echo -e "\033[41;34m此站点${newsite}部署好.\033[0m\n\033[41;34m本地访问此站点${newsite}返回状态码：${TESTSITE}.\033[0m"
            else
                echo -e "\033[41;34m此站点${newsite}部署好.\033[0m\n\033[41;34m本地访问此站点${newsite}返回状态码：${TESTSITE}.\033[0m"
            fi
        }
        fi
    fi
done < $deploysitefile
echo -e "\033[40;32mTotal deployments：${a}个.\033[0m\n\033[41;34mSuccessed：${b}个.\033[0m\n\033[41;39mAccess successed：${c}个\033[0m.\n\033[43;31mFailed：$[a-b]个.\033[0m\n\033[43;32mAccess Failed：$[a-c]个.\033[0m"
end=$(date +%s) 
difference=$(( end - start )) 
echo -e "\033[43;31m部署站点所用的时间：$difference seconds.\033[0m"
echo -e "\033[44;33m最后一定不要忘记在阿里云数据库RDS(香港区域)上创建数据库，部署完后还要在两台seo上nginx -s reload一下，测试能不能访问就OK了!!!!!!\033[0m\n\033[44;33m本地测试命令:\033[0m\n\t\033[44;33mcurl -vL http://localhost/e/index.php -H "Host:www.xinggang5.com"\033[0m\n  \t\033[44;33mcurl -vL http://localhost -H "Host:www.xinggang56.com"\033[0m\n  \t\033[44;33m创建的数据库请在文件${deploysitedb}中查看.\033[0m"
# curl -vL http://localhost/e/index.php -H "Host:www.xinggang5.com"
# curl -vL http://localhost -H "Host:www.xinggang56.com"
